{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "56cb055a-ab0d-4494-a333-f679314d3325",
   "metadata": {},
   "source": [
    "Chapter 06\n",
    "\n",
    "# 基底转换\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31a1ac1c-0105-40b3-a22d-bb4d6426d039",
   "metadata": {},
   "source": [
    "这段代码的核心任务是研究**向量在线性变换下的坐标变换**，并将其在两个不同基底中进行表示：一个是**标准正交基**，另一个是一个**非标准但正交的基底 $V$**。整个过程结合了**线性变换**、**基变换**与**向量坐标表示**等概念，下面我们从数学视角详细解读各步骤。\n",
    "\n",
    "---\n",
    "\n",
    "首先，定义了一个线性变换矩阵：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix}\n",
    "-1 & 0 \\\\\n",
    "0 & -1\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这个矩阵是一个**中心对称变换（central inversion）**，它将任意二维向量 $\\mathbf{v} \\in \\mathbb{R}^2$ 映射到其相反向量 $-\\mathbf{v}$，也就是关于原点的对称。该变换具有如下性质：\n",
    "\n",
    "- 是正交矩阵：$A^\\top A = I$\n",
    "- 行列式为正：$\\det(A) = 1$\n",
    "- 自伴算子：$A = A^\\top$\n",
    "\n",
    "接下来定义一个向量 $\\mathbf{a}$，其在基底 $A$ 所对应的坐标为：\n",
    "\n",
    "$$\n",
    "\\mathbf{x}_A = \\begin{bmatrix} -3 \\\\ -4 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这里的“在 $A$ 中的坐标”其实指的是 $\\mathbf{a}$ 在标准正交基下的坐标值，尚未变基。我们接下来用 $A \\cdot \\mathbf{x}_A$ 得到该向量在**标准正交基**下被 $A$ 作用后的结果：\n",
    "\n",
    "$$\n",
    "A \\mathbf{x}_A = \\begin{bmatrix} -1 & 0 \\\\ 0 & -1 \\end{bmatrix} \\begin{bmatrix} -3 \\\\ -4 \\end{bmatrix} = \\begin{bmatrix} 3 \\\\ 4 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "也就是说，$\\mathbf{x}_A$ 表示的是原始向量 $\\mathbf{a}$，而 $A \\mathbf{x}_A$ 是它变换后的向量（即被 $A$ 作用之后的向量）。\n",
    "\n",
    "---\n",
    "\n",
    "接下来引入了一个新的正交基底 $V$，定义为：\n",
    "\n",
    "$$\n",
    "V = \\begin{bmatrix}\n",
    "\\frac{3}{5} & -\\frac{4}{5} \\\\\n",
    "\\frac{4}{5} & \\frac{3}{5}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这个矩阵满足 $V^\\top V = I$，即它是一个**正交矩阵**，可以看作是将标准基 $\\{\\mathbf{e}_1, \\mathbf{e}_2\\}$ 旋转一定角度得到的一组新基底 $\\{\\mathbf{v}_1, \\mathbf{v}_2\\}$，并且这组基底保持正交与单位长度。\n",
    "\n",
    "由于 $V$ 是正交矩阵，其逆矩阵 $V^{-1}$ 等于其转置矩阵：\n",
    "\n",
    "$$\n",
    "V^{-1} = V^\\top = \\begin{bmatrix}\n",
    "\\frac{3}{5} & \\frac{4}{5} \\\\\n",
    "-\\frac{4}{5} & \\frac{3}{5}\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "最后，计算 $\\mathbf{a}$ 在新的基底 $V$ 中的坐标。为了得到这个坐标，先应用线性变换 $A$，再将结果从标准基转换为 $V$ 基。变换过程如下：\n",
    "\n",
    "1. 计算变换后向量：$\\mathbf{a}' = A \\cdot \\mathbf{x}_A$\n",
    "2. 将其投影到新基底 $V$ 中：$\\mathbf{x}_V = V^{-1} \\cdot \\mathbf{a}'$\n",
    "\n",
    "综上，计算过程为：\n",
    "\n",
    "$$\n",
    "\\mathbf{x}_V = V^{-1} A \\mathbf{x}_A\n",
    "$$\n",
    "\n",
    "代入具体数值进行计算：\n",
    "\n",
    "$$\n",
    "\\mathbf{x}_V = \\begin{bmatrix}\n",
    "\\frac{3}{5} & \\frac{4}{5} \\\\\n",
    "-\\frac{4}{5} & \\frac{3}{5}\n",
    "\\end{bmatrix}\n",
    "\\begin{bmatrix}\n",
    "-1 & 0 \\\\\n",
    "0 & -1\n",
    "\\end{bmatrix}\n",
    "\\begin{bmatrix}\n",
    "-3 \\\\\n",
    "-4\n",
    "\\end{bmatrix}\n",
    "= \\begin{bmatrix}\n",
    "\\frac{3}{5} & \\frac{4}{5} \\\\\n",
    "-\\frac{4}{5} & \\frac{3}{5}\n",
    "\\end{bmatrix}\n",
    "\\begin{bmatrix}\n",
    "3 \\\\\n",
    "4\n",
    "\\end{bmatrix}\n",
    "= \\begin{bmatrix}\n",
    "\\frac{25}{5} \\\\\n",
    "0\n",
    "\\end{bmatrix}\n",
    "= \\begin{bmatrix}\n",
    "5 \\\\\n",
    "0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "这意味着，在新的基底 $V$ 中，向量 $\\mathbf{a}$（在 $A$ 作用后）只包含第一个基向量方向的分量，完全落在 $\\mathbf{v}_1$ 的方向上。\n",
    "\n",
    "---\n",
    "\n",
    "从几何意义上，这段代码演示了一个经典的线性代数操作流程：\n",
    "\n",
    "1. 给定一个线性变换 $A$；\n",
    "2. 给定一个向量 $\\mathbf{a}$；\n",
    "3. 在标准正交基下施加 $A$；\n",
    "4. 切换到一个新的正交基底 $V$；\n",
    "5. 计算向量在该新基底下的表示。\n",
    "\n",
    "它把抽象的矩阵乘法具体化为“坐标变化 + 基变换”的组合，体现了线性代数中坐标、基底、线性变换之间的深刻联系。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f47cc21c-14c2-4c99-8ad9-b3f7b446b3b3",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "46e60434-c1e1-46f8-ba27-ed29b43c2d8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b190e47c-7ba8-46a8-8987-cb9977493402",
   "metadata": {},
   "source": [
    "## 定义基底 A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "deffe1e9-18f8-4d74-83c9-8da957051baa",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[-1, 0],\n",
    "             [0, -1]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7c9ec9e-717e-4d24-a7ef-4b460f3d55e7",
   "metadata": {},
   "source": [
    "## 向量 a 在 A 中坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "61520f72-fe36-441f-99c8-1b500d973438",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_A = np.array([[-3],\n",
    "                [-4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b7de6a28-e2ab-484e-a6ff-452ce58bce5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ x_A\n",
    "# 在标准正交基坐标"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16d33cd0-5364-491b-8c62-c54d48e6f579",
   "metadata": {},
   "source": [
    "## 定义基底 V"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7ca53065-0eac-4b97-acda-fabe71f4ee21",
   "metadata": {},
   "outputs": [],
   "source": [
    "V = np.array([[3/5, -4/5],\n",
    "              [4/5, 3/5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "97e6d052-9a97-4661-8442-830c24c0c397",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.6,  0.8],\n",
       "       [-0.8,  0.6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "V_inv = np.linalg.inv(V)\n",
    "V_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87588bad-419f-456d-99c8-de3c48944f90",
   "metadata": {},
   "source": [
    "## 计算向量 a 在 V 中坐标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f1594467-e68c-4faf-9c9e-92e9c0dab3b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 5.0000000e+00],\n",
       "       [-8.8817842e-16]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_V = V_inv @ A @ x_A\n",
    "x_V"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d836cbb4-49ca-45bd-8f73-f11d71a13ac9",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
